'use strict';
app.controller('sellSetingCtrl',['$scope','utilService','sellSetingService','$modal','toaster','$stateParams', function($scope,utilService,sellSetingService,$modal,toaster,$stateParams) {

    $scope.sellSetingService=sellSetingService;

    $scope.find=function(){
        sellSetingService.find($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.sellSetingService.data=result.data;
                    console.info($scope.sellSetingService.data)
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    $scope.find();

    $scope.update=function(){
        sellSetingService.update($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.find();
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    $scope.select = function(id,isOpenSell){
        console.info(isOpenSell);
        if(isOpenSell == 0){
            var param = {
                id:id,
                isOpenSell: isOpenSell,
            };
            // query data.
            utilService.modal($modal, $scope, {
                title: "警告",
                controller: "updateSellCtrl",
                message: "警告:如把平台分销关闭,系统会把平台下的商品及商家状态都改为【关闭分销】",
                data: param
            });
        }
    }


}]);

app.controller('updateSellCtrl', ['$scope', '$modalInstance', '$state', 'sellSetingService', 'toaster', 'modalData',
    function ($scope, $modalInstance, $state, sellSetingService, toaster, modalData) {
        $scope.modal = modalData;
        $scope.sell = {
            id: modalData.data.id,
            isOpenSell: modalData.data.isOpenSell
        };
        console.info( $scope.sell)
        $scope.AllowToDelete = true;
        $scope.ok = function () {
            sellSetingService.update($scope).then(function(result){
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $scope.cancel();
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break
                }
            });
        }

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);